//
//  TestCollectionViewWindowController.m
//  MacDevTest
//
//  Created by lbxia on 2020/8/16.
//  Copyright © 2020 com.lbx. All rights reserved.
//

#import "TestCollectionViewWindowController.h"
#import "customCollectionViewItem.h"
#import "JJScroller.h"

@interface TestCollectionViewWindowController ()<NSCollectionViewDelegate,NSCollectionViewDataSource>
@property (weak) IBOutlet NSScrollView *scrollView;
@property (weak) IBOutlet NSCollectionView *collectionView;
@property (nonatomic, strong) NSCollectionViewFlowLayout* flowLayout;



@end

@implementation TestCollectionViewWindowController

- (void)awakeFromNib
{
    [super awakeFromNib];
//    https://www.jianshu.com/p/64662db3f3e5
    
    
    _scrollView.borderType = NSNoBorder;
    
    //自定义滚动条，没有背景色，静止时，自动隐藏
    _scrollView.verticalScroller = [[JJScroller alloc]init];
    _scrollView.hasVerticalScroller  = YES;
    
    self.flowLayout = [[NSCollectionViewFlowLayout alloc]init];
    self.flowLayout.itemSize = NSMakeSize(self.collectionView.frame.size.width, 70);
     self.flowLayout.minimumLineSpacing = 0;
     self.flowLayout.minimumInteritemSpacing = 0;
     self.collectionView.collectionViewLayout = self.flowLayout;
   
    //nib形式，需要在xib上面添加一个object,
//     NSNib *nib = [[NSNib alloc]initWithNibNamed:@"customCollectionViewItem" bundle:nil];
//     [self.collectionView registerNib:nib forItemWithIdentifier:@"chatItem"];
    
    [self.collectionView registerClass:[customCollectionViewItem class] forItemWithIdentifier:@"chatItem"];
    
    //设置了，cell点击才有delegate反馈
    self.collectionView.selectable = YES;
    
}

- (void)windowDidLoad {
    [super windowDidLoad];
    
}


- (NSInteger)collectionView:(NSCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 22;
}

- (NSCollectionViewItem *)collectionView:(NSCollectionView *)collectionView itemForRepresentedObjectAtIndexPath:(NSIndexPath *)indexPath
{
//    let item = collectionView.makeItem(withIdentifier: "ChatIem", for: indexPath) as! ChatIem
//           item.model = messageData[indexPath.item]
//           return item
    
    customCollectionViewItem *item = [collectionView makeItemWithIdentifier:@"chatItem" forIndexPath:indexPath];
    
    return item;
}

//如果未调用，可能底层View拦截了mouseDown事件
- (void)collectionView:(NSCollectionView *)collectionView didSelectItemsAtIndexPaths:(NSSet<NSIndexPath *> *)indexPaths
{
    
    for (NSIndexPath *indexpath in indexPaths) {
        
        NSInteger row = indexpath.item;
        NSInteger column = indexpath.section;
        
        NSLog(@"didselect row:%ld,col:%ld",(long)row,(long)column);
        
    }
    
//    NSLog(@"NSCollectionView didSelectItemsAtIndexPaths:%@",indexPaths);
}



@end
